package com.atsmart.app.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.net.Uri;

public class ImageMerger {

	private static ImageMerger instance = null;
	private boolean isFile = false;
	private Uri[] uri = new Uri[5];
	private int currentID = -1;
	private static int[] snapPreview_dat = null;
	private SnapPreview previewProvider = new SnapPreview();
	public static ImageMerger getInstance() {
		if (instance == null){
			instance = new ImageMerger();
		}
		return instance;
	}

	public void release() {
		releaseUri();
		instance = null;
	}

	public void releaseUri() {
		for (int i = 0; i < 5; i++)
			uri[i] = null;
	}

	public void deleteAllBitmap(Context thisContext) {
		for (int i = 1; i < 5; i++) {
			delelteBitmap(i, thisContext);
		}
	}

	public void deleteOtherBitmap(Context thisContext) {
		for (int i = 1; i < 5; i++) {
			if (currentID != i)
				delelteBitmap(i, thisContext);
		}
	}

	private void delelteBitmap(int n, Context thisContext) {
		if (uri[n] == null)
			return;

		Uri si = Uri.parse(uri[n].toString());
		thisContext.getContentResolver().delete(si, null, null);
		uri[n] = null;
	}

	public void Heibai() {
		if (_shotimage == null || _shotimage.isRecycled())
			return;
		int w = _shotimage.getWidth();
		int h = _shotimage.getHeight();
		try {
			snapPreview_dat = new int[w * h];
			_shotimage.getPixels(snapPreview_dat, 0, w, 0, 0, w, h);
			releasedShot();
		} catch (OutOfMemoryError oome) {
		} catch (Exception e) {
		}

		if (snapPreview_dat == null)
			return;

		previewProvider.Heibai(snapPreview_dat, w, h);

		try {
			_mergeImage = Bitmap.createBitmap(snapPreview_dat, w, h,
					Bitmap.Config.RGB_565);
		} catch (OutOfMemoryError ex) {
			System.gc();
		}

		snapPreview_dat = null;
		System.gc();
	}

	public void XRAY() {
		if (_shotimage == null || _shotimage.isRecycled())
			return;
		int width = _shotimage.getWidth();
		int height = _shotimage.getHeight();

		float f[] = { 3.0F, 0.0F, 0.0F, 0.0F, -254F, 0.0F, 3.0F, 0.0F, 0.0F,
				-254F, 0.0F, 0.0F, 3.0F, 0.0F, -254F, 0.0F, 0.0F, 0.0F, 1.0F,
				0.0F };
		ColorMatrix cm = new ColorMatrix(f);
		ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(
				cm);

		Paint paint = new Paint();
		paint.setColorFilter(colorMatrixFilter);
		try {
			_mergeImage = Bitmap.createBitmap(width, height,
					Bitmap.Config.ARGB_8888);
		} catch (OutOfMemoryError ex) {
			System.gc();
		}
		if (_mergeImage == null)
			return;
		Canvas canvas = new Canvas(_mergeImage);
		canvas.drawBitmap(_shotimage, 0, 0, paint);

		releasedShot();
	}

	public void Nuanse() {
		if (_shotimage == null || _shotimage.isRecycled())
			return;
		int w = _shotimage.getWidth();
		int h = _shotimage.getHeight();
		try {
			snapPreview_dat = new int[w * h];
			_shotimage.getPixels(snapPreview_dat, 0, w, 0, 0, w, h);
			releasedShot();
		} catch (OutOfMemoryError oome) {
		} catch (Exception e) {
		}
		if (snapPreview_dat == null)
			return;
		previewProvider.Nuanse(snapPreview_dat, w, h);
		try {
			_mergeImage = Bitmap.createBitmap(snapPreview_dat, w, h,
					Bitmap.Config.RGB_565);
		} catch (OutOfMemoryError ex) {
			System.gc();
		}

		snapPreview_dat = null;
		System.gc();
	}

	public void Anjiao() {
		if (_shotimage == null || _shotimage.isRecycled())
			return;
		int w = _shotimage.getWidth();
		int h = _shotimage.getHeight();
		try {
			snapPreview_dat = new int[w * h];
			_shotimage.getPixels(snapPreview_dat, 0, w, 0, 0, w, h);
			releasedShot();
		} catch (OutOfMemoryError oome) {
		} catch (Exception e) {
		}

		if (snapPreview_dat == null)
			return;

		previewProvider.Anjiao(snapPreview_dat, w, h);
		try {
			_mergeImage = Bitmap.createBitmap(snapPreview_dat, w, h,
					Bitmap.Config.RGB_565);
		} catch (OutOfMemoryError ex) {
			ex.printStackTrace();
		}

		snapPreview_dat = null;
		System.gc();
		// return _mergeImage;
	}

	public static Bitmap _shotimage = null;
	public static Bitmap _mergeImage = null;

	public void releaseBitmap() {
		if (_shotimage != null && !_shotimage.isRecycled()) {
			_shotimage.recycle();
		}
		_shotimage = null;
		if (_mergeImage != null && !_mergeImage.isRecycled()) {
			_mergeImage.recycle();
		}
		_mergeImage = null;
	}

	public static void releasedShot() {
		if (_shotimage != null && !_shotimage.isRecycled()) {
			_shotimage.recycle();
		}
		_shotimage = null;
	}

	public void setFile(boolean isFile) {
		this.isFile = isFile;
	}

	public boolean isFile() {
		return isFile;
	}

	public void setUri(Uri _uri, int n) {
		this.uri[n] = _uri;
	}

	public Uri getUri(int n) {
		return uri[n];
	}

	public void setCurrentID(int currentID) {
		this.currentID = currentID;
	}

	public int getCurrentID() {
		return currentID;
	}
}
